package com.wei.spring.web.user.controller;

import com.wei.spring.web.user.controller.form.UserInfoForm;
import com.wei.spring.web.user.exception.BusinessException;
import com.wei.spring.web.user.model.dto.ResponseCodeEnum;
import com.wei.spring.web.user.model.vo.ResultData;
import com.wei.spring.web.user.model.vo.UserSummaryVO;
import com.wei.spring.web.user.service.UserService;
import com.wei.spring.web.user.util.FileUtil;
import com.wei.spring.web.user.util.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户类
 *
 * @author buhuan.wang
 * @since 2020/12/20
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public ResultData create(@RequestParam(required = true) String config,
                             @RequestBody(required = true) UserInfoForm info) {
        return ResultUtil.getSuccess(
                "create success");
    }

    @RequestMapping("/update")
    @ResponseBody
    public void update(@RequestParam(required = true) String config) {
    }

    @RequestMapping(value = "/list", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public List<UserSummaryVO> list(@RequestParam(required = true) Long id,
                                    @RequestParam(required = true) String name,
                                    @RequestParam(required = true) String desc) {
        List<UserSummaryVO> userSummaryVOList = new ArrayList<>();
        UserSummaryVO user1 = new UserSummaryVO();
        user1.setId(1L);
        user1.setName("Tom");
        UserSummaryVO user2 = new UserSummaryVO();
        user2.setId(id);
        user2.setName(name);
        userSummaryVOList.add(user1);
        userSummaryVOList.add(user2);
        return userSummaryVOList;
    }

    @RequestMapping("/detail")
    @ResponseBody
    public UserSummaryVO detail(@RequestParam(required = true) String id) {
        UserSummaryVO user1 = new UserSummaryVO();
        user1.setId(1L);
        user1.setName("Tom");
        return user1;
    }

    @RequestMapping("/delete")
    @ResponseBody
    public String delete(@RequestParam(required = true) String id,
                         @RequestParam(required = true) String desc) {
        throw new BusinessException(ResponseCodeEnum.TOO_LONG_PARAMETER, "name", 255);
    }

    @RequestMapping("/findId")
    @ResponseBody
    public Long findId(@RequestParam(required = true) String name) {
        return 1L;
    }

    @RequestMapping("/check")
    @ResponseBody
    public boolean check(@RequestParam(required = false) String name) {
        return userService.check(name);
    }

    @RequestMapping("/uploadImage")
    @ResponseBody
    public String uploadImage(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return "上传失败，请选择文件";
        }


        String fileName = file.getOriginalFilename();
        String filePath = "./data/";
        FileUtil.createDir(filePath);
        File dest = new File(filePath + fileName);
        try {
            file.transferTo(dest);
            log.info("上传成功");
            return "上传成功";
        } catch (IOException e) {
            log.error(e.toString(), e);
        }
        return "上传失败！";

    }
}
